﻿using System;
using System.Collections.Generic;
using System.Text;
using jQueryCSharp.Extensions;

namespace jQueryCSharp.ui
{
    public class Tabs : jQueryCSharpBase
    {
        #region Constantes
        private const bool CACHE = false;
        private const bool COLLAPSIBLE = false;
        private const object COOKIE = null;
        private const bool DESELECTABLE = false;
        private const int SELECTED = 0;
        private const string TABSDISABLED = "";

        private const string EVENT = "click";
        private const string IDPREFIX = "ui-tabs-";
        private const string PANELTEMPLATE = "<div></div>";
        private const string SPINNER = "<em>Loading&#8230;</em>";
        private const string TABTEMPLATE = "<li><a href=\"#{href}\"><span>#{label}</span></a></li>";
        #endregion

        /// <summary>
        /// Construtor
        /// </summary>
        public Tabs( ): this("")
        {
        }

        /// <summary>
        /// Construtor
        /// </summary>
        public Tabs( string identifier )
        {
            Disabled = new List<int>( );
            Cache = CACHE;
            Collapsible = COLLAPSIBLE;
            Cookie = COOKIE;
            Deselectable = DESELECTABLE;
            TabsDisabled = TABSDISABLED;
            Event = EVENT;
            IdPrefix = IDPREFIX;
            PanelTemplate = PANELTEMPLATE;
            Selected = SELECTED;
            Spinner = SPINNER;
            TabTemplate = TABTEMPLATE;

            if ( !String.IsNullOrEmpty( identifier ) )
            {
                if ( identifier.Contains( "#" ) )
                {
                    ID = identifier.Replace( "#", "" );
                }
                else
                {
                    Class = identifier.Replace( ".", "" );
                }
            }
        }

        #region Propriedades
        public IList<int> Disabled { get; set; }
        public bool Cache { get; set; }
        public bool Collapsible { get; set; }
        public object Cookie { get; set; }
        public bool Deselectable { get; set; }
        public string TabsDisabled { get; set; }
        public string Event { get; set; }
        public string IdPrefix { get; set; }
        public string PanelTemplate { get; set; }
        public int Selected { get; set; }
        public string Spinner { get; set; }
        public string TabTemplate { get; set; }
        #endregion

        #region Override

        public override string ToString( )
        {
            var sb = new StringBuilder( );

            if ( Disabled.Count > 0 )
            {
                const string modeloArray = "[{0}]";
                sb.Append( modelo.Formatar( "disabled", modeloArray.Formatar( Disabled.ToString( ) ) ) );
            }

            if ( Cache != CACHE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "cache", Cache.ToString( ).ToLower( ) ) );
            }

            if ( Collapsible != COLLAPSIBLE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "collapsible", Collapsible.ToString( ).ToLower( ) ) );
            }

            if ( Cookie != COOKIE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                //sb.Append( "cookie: { expires: 30 } }" );
                //sb.Append( modelo.Formatar( "cookie", "'{0}'".Formatar( Cookie ) ) );
            }

            if ( Deselectable != DESELECTABLE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "deselectable", Deselectable.ToString( ).ToLower( ) ) );
            }

            if ( TabsDisabled != TABSDISABLED )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "disabled", "'{0}'".Formatar( TabsDisabled ) ) );
            }

            if ( Event != EVENT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "event", "'{0}'".Formatar( Event ) ) );
            }

            if ( IdPrefix != IDPREFIX )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "idPrefix", "'{0}'".Formatar( IdPrefix ) ) );
            }

            if ( PanelTemplate != PANELTEMPLATE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "PanelTemplate", "'{0}'".Formatar( PanelTemplate ) ) );
            }

            if ( Selected != SELECTED )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "selected", Selected.ToString( ) ) );
            }

            if ( Spinner != SPINNER )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "spinner", "'{0}'".Formatar( Spinner ) ) );
            }

            if ( TabTemplate != TABTEMPLATE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "tabTemplate", "'{0}'".Formatar( TabTemplate ) ) );
            }


            if ( sb.Length > 0 )
            {
                return String.Concat( "{", sb.ToString( ), "}" );
            }

            return sb.ToString( );
        }

        #endregion
    }
}
